package com.muyu.system.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.muyu.common.core.constant.CommonConstant;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.enums.PayStatus;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.remote.PayFeign;
import com.muyu.system.danmo.dto.PayDTO;
import com.muyu.system.danmo.vo.PayVO;
import com.muyu.system.domain.DanmoVipPackage;
import com.muyu.system.domain.DanmoVipPerson;
import com.muyu.system.mapper.DanmoVipPackageMapper;
import com.muyu.system.mapper.DanmoVipPersonMapper;
import com.muyu.system.service.IDanmoVipPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.muyu.common.core.constant.Constants.MCH_CODE;

/**
 * vip人员Service业务层处理
 * 
 * @author yl
 * @date 2025-06-10
 */
@Service
public class DanmoVipPersonServiceImpl implements IDanmoVipPersonService
{
    @Autowired
    private DanmoVipPersonMapper danmoVipPersonMapper;
    @Autowired
    private DanmoVipPackageMapper danmoVipPackageMapper;

@Autowired
private PayFeign payFeign;

    /**
     * 查询vip人员
     * 
     * @param id vip人员主键
     * @return vip人员
     */
    @Override
    public DanmoVipPerson selectDanmoVipPersonById(Long id)
    {
        return danmoVipPersonMapper.selectDanmoVipPersonById(id);
    }

    /**
     * 查询vip人员列表
     * 
     * @param danmoVipPerson vip人员
     * @return vip人员
     */
    @Override
    public List<DanmoVipPerson> selectDanmoVipPersonList(DanmoVipPerson danmoVipPerson)
    {
        return danmoVipPersonMapper.selectDanmoVipPersonList(danmoVipPerson);
    }

    /**
     * 新增vip人员
     *
     * @param danmoVipPerson vip人员
     * @return 结果
     */
    @Override
    public Result<PayVO> insertDanmoVipPerson(DanmoVipPerson danmoVipPerson)
    {


        danmoVipPerson.setId(IdWorker.getId());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        danmoVipPerson.setPayTime(DateUtils.getNowDate());
        danmoVipPerson.setCreateTime(DateUtils.getNowDate());
        danmoVipPerson.setCreateBy(loginUser.getUsername());
        danmoVipPerson.setPayStatus(PayStatus.PENDING);
        danmoVipPerson.setPayType(danmoVipPerson.getPayType());
        danmoVipPerson.setUserId(SecurityUtils.getUserId());
        danmoVipPerson.setOrderSn(CommonConstant.VIP_TOP+RandomUtil.randomNumbers(16));
        Long vipPackageId = danmoVipPerson.getVipPackageId();
        DanmoVipPackage danmoVipPackage = danmoVipPackageMapper.selectDanmoVipPackageById(vipPackageId);
        danmoVipPerson.setPayAmount(danmoVipPackage.getPrice());

        PayDTO pay = new PayDTO();
        pay.setProjectName("VIP");
        pay.setMchCode(MCH_CODE);
        pay.setPayMethod(danmoVipPerson.getPayType());
        pay.setBizOrderNo(danmoVipPerson.getOrderSn());
        pay.setDescription("VIP");
        pay.setTotalAmount( danmoVipPerson.getPayAmount());
        pay.setTimeExpire(DateUtil.offsetMinute(DateUtil.date(), 10).toLocalDateTime());
        pay.setPayTel(Long.valueOf(loginUser.getSysUser().getPhonenumber()));
        Result<PayVO> result = payFeign.pay(pay);
        danmoVipPerson.setPayOrderNo(result.getData().getOrderNo());
        danmoVipPerson.setPayStatus(PayStatus.PENDING);
        int i = danmoVipPersonMapper.insertDanmoVipPerson(danmoVipPerson);
        return Result.success(result.getData(),"添加成功");
    }

    /**
     * 修改vip人员
     *
     * @param danmoVipPerson vip人员
     * @return 结果
     */
    @Override
    public Result<Integer> updateDanmoVipPerson(DanmoVipPerson danmoVipPerson)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        danmoVipPerson.setUpdateTime(DateUtils.getNowDate());
        danmoVipPerson.setUpdateBy(loginUser.getUsername());
        int i = danmoVipPersonMapper.updateDanmoVipPerson(danmoVipPerson);
        return Result.success(i, "修改成功");
    }

    /**
     * 批量删除vip人员
     * 
     * @param ids 需要删除的vip人员主键
     * @return 结果
     */
    @Override
    public int deleteDanmoVipPersonByIds(Long[] ids)
    {
        return danmoVipPersonMapper.deleteDanmoVipPersonByIds(ids);
    }

    /**
     * 删除vip人员信息
     * 
     * @param id vip人员主键
     * @return 结果
     */
    @Override
    public int deleteDanmoVipPersonById(Long id)
    {
        return danmoVipPersonMapper.deleteDanmoVipPersonById(id);
    }

    @Override
    public DanmoVipPerson selectDanmoVipPerson(DanmoVipPerson danmoVipPerson) {
        return danmoVipPersonMapper.selectDanmoVipPerson(danmoVipPerson);
    }
}
